
function modalClose(ev) {
    $(ev).closest('#senateEdit').hide();
}


// 检查参数是否为json格式
function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
                return true;
            } else {
                return false;
            }
        } catch (e) {
            console.log('error：' + str + '!!!' + e);
            return false;
        }
    }
    console.log('It is not a string!')
}



// 将方法绑定到jquery 上
$.extend({
    showNotice: showNotice,
    //
    loginOut: loginOut,
    //
    isJSON: function (str) {
        if (typeof str != 'string') {
            return false;
        }
        try {
            const obj = JSON.parse(str);
            return (typeof obj == 'object' && obj);
        } catch (e) {
            console.log('error：' + str + '!!!' + e);
            return false;
        }
    },

});

function showNotice(level, content) {
    debugger
    const html = `
        <div id="notice" class="position-fixed top-0 end-0 p-3">
            <div class="alert alert-success alert-dismissible">
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                <strong id="level">@level@</strong> <span id="content">@content@</span>
            </div>
        </div>
        `;

    const showHtml = html.replace("@level@", level).replace("@content@", content);
    $('body').append(showHtml);
}


function loginOut(level, content){
    // console.log("-- loginOut --")
    // const url = window.location;
    // const arr = url.pathname.split(location.host);
    // arr.pop();
    // arr.push('');
    // const newUrl = url.origin + arr.join('/') + url.search;
    // history.pushState('', '', newUrl);
    // console.log("----", newUrl)

    $.ajax({
        type: "get",
        url: '/login/out',
        async: true,
        data: JSON.stringify({}),
        dataType: "json",
        success: function (result) {
            console.warn(result);
            // const h = location.host;
            // const p = location.port;
            // location.href = 'http://'+location.host+"/";
        },
        error: function (msg) {
            console.warn(msg);
            // $.showNotice("ERROR","退出登录失败： " + JSON.stringify(msg));
        }
    });
}